﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace KorbeRTR.Models
{
    public class CutRegion
    {
        public delegate int GetLayerInvoker(object param);

        /// <summary>
        /// 报警内容
        /// </summary>
        public string Context;

        /// <summary>
        /// 切图图层. -1 不切图
        /// </summary>
        public GetLayerInvoker GetCutLayer;

        /// <summary>
        /// 参数
        /// </summary>
        public object Param;

        /// <summary>
        /// 切图区域
        /// </summary>
        public Rectangle Rect;

        public CutRegion()
        {
            GetCutLayer = (object param) => { return -1; };
            Rect = new Rectangle();
        }

        public CutRegion(Rectangle rect, GetLayerInvoker Invoker=null)
        {
            Rect = rect;

            GetCutLayer = (object param) => { return -1; };
            if (Invoker != null)
            {
                GetCutLayer = Invoker;
            }
        }
    }

    public class TextRegion
    {
        public delegate Tuple<string, Color> GetTextInvoker(object param);

        /// <summary>
        /// 切图图层. -1 不切图
        /// </summary>
        public GetTextInvoker GetText;

        /// <summary>
        /// 参数
        /// </summary>
        public object Param;

        /// <summary>
        /// 切图区域
        /// </summary>
        public Rectangle Rect;

        public TextRegion()
        {
            GetText = (object param) => { return null; };
            Rect = new Rectangle();
        }

        public TextRegion(Rectangle rect, GetTextInvoker Invoker = null)
        {
            Rect = rect;

            GetText = (object param) => { return null; };
            if (Invoker != null)
            {
                GetText = Invoker;
            }
        }
    }

    public class CutPicture
    {
        /// <summary>
        /// 图层
        /// </summary>
        public List<Bitmap> PictureLayers { get; private set; }
        
        /// <summary>
        /// 剪切区域
        /// </summary>
        public List<CutRegion> CutRegions { get; private set; }

        public List<TextRegion> TextRegions { get; private set; }

        public CutPicture()
        {
            PictureLayers = new List<Bitmap>();

            CutRegions = new List<CutRegion>();

            TextRegions = new List<TextRegion>();
        }

        public Bitmap Get()
        {
            if (PictureLayers.Count == 0)
            {
                return null;
            }

            Bitmap canvas = new Bitmap(PictureLayers[0].Width, PictureLayers[0].Height);
            Graphics g = Graphics.FromImage(canvas);
            g.DrawImage(PictureLayers[0], 0, 0, PictureLayers[0].Width, PictureLayers[0].Height);

            foreach (var region in CutRegions)
            {
                int index = region.GetCutLayer(region.Param);
                if (index < 0) continue;

                if (index >= PictureLayers.Count) continue;

                var layer = PictureLayers[index];

                var tmp = layer.Clone(region.Rect, layer.PixelFormat);

                g.DrawImage(tmp, region.Rect);
            }

            foreach (var region in TextRegions)
            {
                var text = region.GetText(region.Param);
                if (text == null) continue;

                //
                g.DrawString(text.Item1, new Font("宋体", 12), new SolidBrush(text.Item2), new RectangleF(region.Rect.X, region.Rect.Y, region.Rect.Width, region.Rect.Height));
            }

            return canvas;
        }

        /// <summary>  
        /// 合并图片  
        /// </summary>  
        /// <param name="imgBack"></param>  
        /// <param name="img"></param>  
        /// <returns></returns>  
        public static Bitmap CombinBitmap(Bitmap imgBack, Bitmap img, int xDeviation = 0, int yDeviation = 0)
        {
            Bitmap bmp = new Bitmap(imgBack.Width, imgBack.Height);
            Graphics g = Graphics.FromImage(bmp);
            g.Clear(Color.White);
            g.DrawImage(imgBack, 0, 0, imgBack.Width, imgBack.Height);

            g.DrawImage(img, xDeviation, yDeviation, img.Width, img.Height);
            GC.Collect();
            return bmp;
        }
    }
}
